import logging
from flask_restful import Resource,reqparse
from flask_babel import _
from controllers.robot import api
from services.errors.robots import RobotNotExistError, RobotParameterError
from services.errors.versiions import FVerTaskResultNotExistError
from services.ota.fver_service import FVerService

class VersionStatusApi(Resource):
    def get(self,robot_id:str,bluetooth_id:str):
        try:
            return FVerService.check_robot_update_task(robot_id,bluetooth_id)
        except Exception as e:
            logging.error(e)
        return {"has":False}
            
    # @hmac_verify
    def put(self,robot_id:str,bluetooth_id:str):
        parser = reqparse.RequestParser()
        parser.add_argument('status',type=str,choices=['U','S','F','T'],required=True,location='json')
        parser.add_argument('task_id',type=str,required=True,location='json')
        args = parser.parse_args()
        status = args["status"]
        task_id = args["task_id"]
        try:
            FVerService.update_fver_task_status(task_id,robot_id,bluetooth_id,status)
            return {"code":200,"message":"success"},200
        except RobotNotExistError as e:
            return {"message":str(e),"code":404},200
        except FVerTaskResultNotExistError as e:
            return {"code":404,"message":e.description},200
        except RobotParameterError as e:
            return {"code":400,"message":e.description},200
        except Exception as e:
            return {"code":500,"message":str(e)},200
        

api.add_resource(VersionStatusApi, '/fireware/<string:robot_id>/<string:bluetooth_id>')